package com.pureut.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.production.domain.CutOrdersDocument;
import com.pureut.production.domain.CutOrdersDocumentDetail;
import com.pureut.production.domain.dto.CutOrdersDocumentDetailDto;
import com.pureut.production.domain.dto.SubVolumePdaDto;
import com.pureut.production.domain.dto.SubvolumeAndroidDto;
import com.pureut.production.domain.vo.CutOrdersDocumentDetailVo;
import com.pureut.production.domain.vo.SubVolumeVo;
import com.pureut.production.domain.vo.SubvolumeAndroidVo;
import com.pureut.production.mapper.CutOrdersDocumentDetailMapper;
import com.pureut.production.mapper.CutOrdersDocumentMapper;
import com.pureut.production.service.CutOrdersDocumentDetailService;
import com.pureut.production.service.CutOrdersDocumentService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 分切制令单明细实现层
 * Author:C
 * Date:2023/02/16 16:16
 * @DESC
 */
@Service
public class CutOrdersDocumentDetailServiceImpl extends ServiceImpl<CutOrdersDocumentDetailMapper, CutOrdersDocumentDetail> implements CutOrdersDocumentDetailService {

    @Resource
    CutOrdersDocumentDetailMapper cutOrdersDocumentDetailMapper;

    @Resource
    CutOrdersDocumentMapper cutOrdersDocumentMapper;

    @Lazy
    @Resource
    CutOrdersDocumentService cutOrdersDocumentService;

    /**
     * 制令单明细列表
     *
     * @param documentNum
     * @return
     */
    @Override
    public List<CutOrdersDocumentDetailDto> getList(String documentNum) {

        //物料等级
        List<SysDictData> judgmentGradeArray = DictUtils.getDictCache("quality_judgment_grade");
        Map<String, String> judgmentGradeMap = judgmentGradeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //验布状态
        List<SysDictData> clothStatusArray = DictUtils.getDictCache("splitting_cloth_inspection_status");
        Map<String, String> clothStatusMap = clothStatusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //分切状态
        List<SysDictData> slittingStatusArray = DictUtils.getDictCache("splitting_slitting_status");
        Map<String, String> slittingStatusMap = slittingStatusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));


        List<CutOrdersDocumentDetailDto> list = cutOrdersDocumentDetailMapper.getList(documentNum);

        for (CutOrdersDocumentDetailDto entity : list) {
            entity.setMaterialLevelDict(judgmentGradeMap.get(entity.getMaterialLevel()));

            entity.setClothInspectionStatusDict(clothStatusMap.get(entity.getClothInspectionStatus()));

            entity.setSlittingStatusDict(slittingStatusMap.get(entity.getSlittingStatus()));
        }
        return list;
    }

    /**
     * 新增
     *
     * @param ctOrdersDocumentDetailVo
     * @return
     */
    @Override
    public boolean add(CutOrdersDocumentDetailVo ctOrdersDocumentDetailVo) {

        CutOrdersDocumentDetail cutOrdersDocumentDetail = new CutOrdersDocumentDetail();

        cutOrdersDocumentDetail.setDocumentNum(ctOrdersDocumentDetailVo.getDocumentNum())
                .setMasterVolumeNum(ctOrdersDocumentDetailVo.getMasterVolumeNum())
                .setMaterialCode(ctOrdersDocumentDetailVo.getMaterialCode())
                .setMaterialLevel(ctOrdersDocumentDetailVo.getMaterialLevel())
                .setClothInspectionStatus(ctOrdersDocumentDetailVo.getClothInspectionStatus())
                .setSlittingStatus(1)
                .setWidth(ctOrdersDocumentDetailVo.getWidth())
                .setRollDiameter(ctOrdersDocumentDetailVo.getRollDiameter())
                .setGramWeight(ctOrdersDocumentDetailVo.getGramWeight())
                .setThickness(ctOrdersDocumentDetailVo.getThickness())
                .setJoint(ctOrdersDocumentDetailVo.getJoint())
                .setClassDifference(ctOrdersDocumentDetailVo.getClassDifference())
                .setRollLength(ctOrdersDocumentDetailVo.getRollLength())
                .setProductNumber(ctOrdersDocumentDetailVo.getProductNumber())
                .setBatchNo(ctOrdersDocumentDetailVo.getBatchNo())
                .setSmallRollWeight(ctOrdersDocumentDetailVo.getSmallRollWeight());

        return save(cutOrdersDocumentDetail);
    }


    /**
     * 根据产品号码获取明细
     *
     * @param productNumber
     * @return
     */
    @Override
    public CutOrdersDocumentDetailDto getView(String productNumber) {
        List<CutOrdersDocumentDetailDto> viewByProductNumber = cutOrdersDocumentDetailMapper.getViewByProductNumber(productNumber);
//        if (viewByProductNumber.size() > 1) {
//            throw new GlobalException("该物料已分切（分卷）");
//        }
        CutOrdersDocumentDetailDto cutOrdersDocumentDetailDto = viewByProductNumber.get(0);
        if ("2".equals(cutOrdersDocumentDetailDto.getSlittingStatus())) {
            throw new GlobalException("该物料已分切（分卷）");
        }
        //获取分切制令单线别
        CutOrdersDocument cutOrdersDocument = cutOrdersDocumentMapper.selectOne(new QueryWrapper<CutOrdersDocument>().lambda().eq(CutOrdersDocument::getSplittingOrderNumber, cutOrdersDocumentDetailDto.getDocumentNum()));

        List<String> strings = Arrays.asList(cutOrdersDocument.getLineTypeName().split(","));
        if (strings.size() == 1) {
            cutOrdersDocumentDetailDto.setLineType(cutOrdersDocument.getLineTypeName());
        }
        if (!("4".equals(cutOrdersDocumentDetailDto.getClothInspectionStatus())) && !("2".equals(cutOrdersDocumentDetailDto.getClothInspectionStatus()))) {
            throw new GlobalException("此母卷未验布");
        }
        return cutOrdersDocumentDetailDto;
    }

    /**
     * 手持pda 分卷
     *
     * @param subvolumeAndroidVo
     * @return
     */
    @Override
    public SubvolumeAndroidDto subvolumeAndroid(SubvolumeAndroidVo subvolumeAndroidVo) throws Exception {

        SubvolumeAndroidDto subvolumeAndroidDto = new SubvolumeAndroidDto();

        //如果是第一步，输入母卷号码
        if (subvolumeAndroidVo.getStep() == 1) {
            try {
                getView(subvolumeAndroidVo.getInputValue());
            } catch (Exception e) {
                subvolumeAndroidDto.setCode("500");
                subvolumeAndroidDto.setMessage("该物料不符合分卷要求，请核实!");
                return subvolumeAndroidDto;
            }
            subvolumeAndroidDto.setCode("200");
            subvolumeAndroidDto.setMessage("操作成功");
            return subvolumeAndroidDto;
        }
        //如果是第二步，执行分卷逻辑
        if (subvolumeAndroidVo.getStep() == 2) {
            CutOrdersDocumentDetailDto view = getView(subvolumeAndroidVo.getMasterVolumeNum());
            SubVolumeVo subVolumeVo = new SubVolumeVo();
            subVolumeVo.setSubVolumeNum(Integer.parseInt(subvolumeAndroidVo.getInputValue()));
            subVolumeVo.setProductNumber(subvolumeAndroidVo.getMasterVolumeNum());
            subVolumeVo.setLineType(subvolumeAndroidVo.getLineCode());
            SubVolumePdaDto subVolumePdaDto = cutOrdersDocumentService.subVolume(subVolumeVo);
            if ("200".equals(subVolumePdaDto.getCode())) {
                subvolumeAndroidDto.setCode("200");
                subvolumeAndroidDto.setMessage("物料号码:" + view.getMasterVolumeNum() + "," + "物料名称:" + view.getMaterialName() + "," + "分卷成功");
            } else {
                subvolumeAndroidDto.setCode("500");
                subvolumeAndroidDto.setMessage("物料号码:" + view.getMasterVolumeNum() + "," + "物料名称:" + view.getMaterialName() + "," + "分卷成功");
            }
            return subvolumeAndroidDto;
        }
        subvolumeAndroidDto.setCode("500");
        subvolumeAndroidDto.setMessage("步骤错误，请核实!");
        return subvolumeAndroidDto;
    }

    /**
     * 查询母卷是否分切
     *
     * @param masterVolumeNumber
     * @return
     */
    @Override
    public CutOrdersDocumentDetailDto getCutOrdersDocumentInfo(String masterVolumeNumber) {
        return cutOrdersDocumentDetailMapper.getCutOrdersDocumentInfo(masterVolumeNumber);
    }
}
